<template>
    <div class="todo-footer" v-if="total">
        <label>
            <input type="checkbox" v-bind:checked="isAll" @click="checkAll"/>
        </label>
        <span>
        <span>已完成{{doneTotal}}</span> / 全部{{total}}
        </span>
        <button class="btn btn-danger" @click="clearAll">清除已完成任务</button>
    </div>
</template>

<script>
    export default {
        name:'MyFooter',
        props:['todos','checkAllTodo','clearAllTodo'],
        // 计算属性
        computed:{
            //完整书写方式
            total:function(){
                return this.todos.length;
            },
            //计算属性的简写方式
            doneTotal() {
                //原始的统计方法
                // let count=0;
                // for (let index = 0; index < this.todos.length; index++) {
                //     if (this.todos[index].done) {
                //         count++;
                //     }
                // }
                // return count;

                //使用原型的reduce条件统计方法，第一个参数为函数体，第二个为统计开始值
                const count=this.todos.reduce((pre,todo)=>{
                    return pre+(todo.done? 1 : 0);
                },0);
                return count;
            },
            isAll(){
                return this.doneTotal===this.total&&this.total>0;//计算属性是可以嵌套的
            }
        },
        methods:{
            checkAll(event){
                console.log(event.target.checked);
                this.checkAllTodo(event.target.checked);
            },
            clearAll(){
                 this.clearAllTodo();
            }
        }
    }
</script>

<style scoped>
    /*footer*/
    .todo-footer {
        height: 40px;
        line-height: 40px;
        padding-left: 6px;
        margin-top: 5px;
    }

    .todo-footer label {
        display: inline-block;
        margin-right: 20px;
        cursor: pointer;
    }

    .todo-footer label input {
        position: relative;
        top: -1px;
        vertical-align: middle;
        margin-right: 5px;
    }

    .todo-footer button {
        float: right;
        margin-top: 5px;
    }
</style>